<?php

namespace app\index\controller;

use think\exception\ValidateException;
use app\model\Store;
use app\model\StoreImage;
use app\model\StoreIncomelog;
use app\model\Order;
use app\model\OrderGoods;
use app\model\Category;

class AdminstoreController extends Base
{

    public function detail()
    {
        $message = '';
        $is_login = 0;
        $weid = weid();

        $data = Store::getInfobyuid(UID());
        if (!empty($data)) {
            if ($data['status'] == 1) {
                $is_login = 1;

                $this->userInfo['sid'] = $data['id'];
                $this->setAppToken($this->userInfo, $this->getAppToken());

                $data['store_logo'] = toimg($data['store_logo']);
                $data['store_banner'] = StoreImage::field('image')
                    ->where(['sid' => $data['id'], 'ptype' => 'banner'])
                    ->order('sort asc')
                    ->select()
                    ->toArray();

                $data['content_img'] = StoreImage::field('image')
                    ->where(['sid' => $data['id'], 'ptype' => 'content'])
                    ->order('sort asc')
                    ->select()
                    ->toArray();

                $where[] = ['weid', '=', $weid];
                $where[] = ['sid', '=', $data['id']];
                $create_time[0] = date("Y-m-d", time());
                $create_time[1] = date("Y-m-d", strtotime("+1 day"));
                $where[] = ['create_time', 'between', [strtotime($create_time[0]), strtotime($create_time[1])]];

                $whereService = $where;
                $data['ordercount'] = Order::where($whereService)->count();

                $data['dayofflinetotal'] = (new Order())->where($whereService)->where('pay_time', '>', 0)->where('payment_code', 'offline_pay')->sum('total');

                $data['StoreIncome'] = StoreIncomelog::where('order_id', 'IN', function ($query) use ($whereService) {
                    $query->name('order')->where($whereService)->field('id');
                })
                    ->sum('income');
            } else {
                $data = 0;
                $message = '请先登录！';
            }
        }

        return $this->json(['message' => $message, 'is_login' => $is_login, 'data' => $data]);
    }

    public function reportformsdetail()
    {

        $data = Store::getInfobyuid(UID());
        $weid = weid();
        if (!empty($data) && $data['status'] == 1) {
            $day = input('post.day', '', 'serach_in');

            if ($day) {

                $where[] = ['weid', '=', $weid];
                $where[] = ['sid', '=', $data['id']];
                $create_time[0] = $day;
                $create_time[1] = date("Y-m-d", strtotime("+1 day", strtotime($day)));
                $where[] = ['create_time', 'between', [strtotime($create_time[0]), strtotime($create_time[1]) - 1]];

                $datadetail['day'] = $day;
                $datadetail['ordercount'] = Order::where($where)->count();

                $datadetail['dayofflinetotal'] = Order::where($where)->where('pay_time', '>', 0)->where('payment_code', 'offline_pay')->sum('total');
                $datadetail['dayofflineordercount'] = Order::where($where)->where('pay_time', '>', 0)->where('payment_code', 'offline_pay')->count();

                $datadetail['daygoodsgiftcardtotal'] = Order::where($where)->where('pay_time', '>', 0)->where('goodsgiftcard_id', '>', 0)->sum('total');
                $datadetail['daygoodsgiftcardordercount'] = Order::where($where)->where('pay_time', '>', 0)->where('goodsgiftcard_id', '>', 0)->count();

                $datadetail['dayreturnOrdertotal'] = Order::where($where)->where('order_status_id', 6)->sum('total');
                $datadetail['dayreturnOrdercount'] = Order::where($where)->where('order_status_id', 6)->count();

                $datadetail['StoreIncome'] = StoreIncomelog::where('order_id', 'IN', function ($query) use ($where) {
                    $query->name('order')->where($where)->field('id');
                })->sum('income');

                $datadetail['StoreIncomecount'] = StoreIncomelog::where('order_id', 'IN', function ($query) use ($where) {
                    $query->name('order')->where($where)->field('id');
                })->count();


                $category = Category::where(['weid' => $weid, 'pid' => 0, 'is_ordercount' => 1])->select()->toArray();
                if ($category) {
                    foreach ($category as &$cvo) {
                        $sonid = Category::getallsonid($cvo['id']);
                        $cvo['daytotal'] = Order::where($where)->where('pay_time', '>', 0)->where('cate_ids', 'IN', $sonid)->sum('total');
                        $cvo['dayordercount'] = Order::where($where)->where('pay_time', '>', 0)->where('cate_ids', 'IN', $sonid)->count();
                        $cvo['piece'] = OrderGoods::where('order_id', 'IN', function ($query) use ($where,$sonid) {
                            $query->name('order')->where($where)->where('cate_ids', 'IN', $sonid)->field('id');
                        })->count();
                    }
                }
                if ($category) {
                    $datadetail['category'] = $category;
                } else {
                    $datadetail['category'] = [];
                }
            }
        }

        return $this->json(['data' => $datadetail]);
    }

    public function reportforms()
    {
        $data = Store::getInfobyuid(UID());
        $weid = weid();
        if (!empty($data) && $data['status'] == 1) {
            $daytype = input('post.daytype', '', 'intval');

            $create_time[0] = date("Y-m-d", time());
            $create_time[1] = date("Y-m-d", strtotime("+1 day"));

            if ($daytype == 0) {
                $cycle = 7;
            } elseif ($daytype == 1) {
                $cycle = date('j');
            } elseif ($daytype == 2) {
                $lastDayOfLastMonth = date('Y-m-t', strtotime('last month'));
                $cycle =  date('j', strtotime($lastDayOfLastMonth));
                $create_time[0] = $lastDayOfLastMonth;
                $create_time[1] = date("Y-m-d", strtotime("+1 day", strtotime($create_time[0])));
            }

            for ($i = 0; $i < $cycle; $i++) {
                if ($i) {
                    $create_time[0] = date("Y-m-d", strtotime("-1 day", strtotime($create_time[0])));
                    $create_time[1] = date("Y-m-d", strtotime("-1 day", strtotime($create_time[1])));
                }
                $datalist[$i]['detail'] = [];
                $datalist[$i]['daytitle'] = $create_time[0];
                $where['weid'] = $weid;
                $where['sid'] = $data['id'];

                $datalist[$i]['daytotal'] = Order::where($where)->where('pay_time', '>', 0)->where('create_time', 'between', [strtotime($create_time[0]), strtotime($create_time[1]) - 1])->sum('total');
            }
        }

        return $this->json(['message' => $message, 'data' => $datalist, 'cycle' => $cycle]);
    }

    public function check()
    {
        $data = Store::getInfobyuid(UID());
        $data['Category'] = Category::gettoparray();

        return $this->json(['data' => $data]);
    }
}
